package pc

import (
	. "chess_platform/internal/common"
	"chess_platform/models/pc"
	"fmt"
	"time"
)

//[pc]抽奖系统管理
type LotteryController struct {
	GrantController
}

func (b *LotteryController) URLMapping() {
	//抽奖奖品:product
	b.Mapping("NewLotteryProduct", b.NewLotteryProduct)
	b.Mapping("UpdateLotteryProduct", b.UpdateLotteryProduct)
	b.Mapping("ListLotteryProduct", b.ListLotteryProduct)

	//钻石消耗记录:consume
	b.Mapping("ListLotteryConsume", b.ListLotteryConsume)

	//钻石兑换抽奖次数:exchange
	b.Mapping("NewLotteryExchange", b.NewLotteryExchange)
	b.Mapping("UpdateLotteryExchange", b.UpdateLotteryExchange)
	b.Mapping("DeleteLotteryExchange", b.DeleteLotteryExchange)
	b.Mapping("ListLotteryExchange", b.ListLotteryExchange)

}

// @Title 添加奖品
// @Description 添加奖品
// @Param   name         body   string     true       "奖品名字"
// @Param   piece        body   int        true       "奖品碎片"
// @Param   prob         body   int        true       "抽中奖品的概率,存储时乘以100"
// @Param   type         body   int        true       "奖品类型,1-钻石,2-非钻石"
// @Param   value        body   int        true       "奖品数量"
// @Success 0 {string}  状态码
// @router /product/new-lottery-product [post]
func (b *LotteryController) NewLotteryProduct() {
	var p LotteryProduct
	if !b.DecodeParams(&p) {
		return
	}
	b.Valid.Required(p.Name, "name").Message("奖品名字不能为空")
	b.Valid.Required(p.Piece, "piece").Message("奖品碎片至少为1")
	b.Valid.Range(p.Status, 0, 1, "status").Message("是否已上线的产品, 1-已上线, 0-未上线")
	b.Valid.Range(p.Prob, 0, 10000, "prob").Message("抽中奖品的概率,存储时乘以100")
	b.Valid.Range(p.Type, 1, 2, "type").Message("奖品类型,1-钻石,2-非钻石")
	b.Valid.Required(p.Value, "value").Message("奖品数量")
	if !b.VerifyParamsError() {
		return
	}
	data := map[string]interface{}{
		"name":        p.Name,
		"piece":       p.Piece,
		"prob":        p.Prob,
		"status":      p.Status,
		"type":        p.Type,
		"value":       p.Value,
		"create_time": time.Now().Unix(),
	}
	_, err := pc.NewLotteryProduct(data)
	if err != nil {
		LogPc.Error(b.DatabaseError(err.Error()))
		return
	}
	msg := fmt.Sprintf("添加奖品(%v)", p.Name)
	b.NewLog(LogLottery, OpNew, msg, data)
	b.ResponseSuccess()
}

// @Title 更新奖品
// @Description 更新奖品
// @Param   id           body   int        true       "奖品id"
// @Param   name         body   string     true       "奖品名字"
// @Param   piece        body   int        true       "奖品碎片"
// @Param   prob         body   int        true       "抽中奖品的概率,存储时乘以100"
// @Param   type         body   int        true       "奖品类型,1-钻石,2-非钻石"
// @Param   value        body   int        true       "奖品数量"
// @Success 0 {string}  状态码
// @router /product/update-lottery-product [post]
func (b *LotteryController) UpdateLotteryProduct() {
	var p LotteryProduct
	if !b.DecodeParams(&p) {
		return
	}
	b.Valid.Required(p.Id, "id").Message("奖品Id不能为0")
	b.Valid.Required(p.Name, "name").Message("奖品名字不能为空")
	b.Valid.Required(p.Piece, "piece").Message("奖品碎片至少为1")
	b.Valid.Range(p.Status, 0, 1, "status").Message("是否已上线的产品, 1-已上线, 0-未上线")
	b.Valid.Range(p.Prob, 0, 10000, "prob").Message("抽中奖品的概率,存储时乘以100")
	b.Valid.Range(p.Type, 1, 2, "type").Message("奖品类型,1-钻石,2-非钻石")
	b.Valid.Required(p.Value, "value").Message("奖品数量")
	if !b.VerifyParamsError() {
		return
	}
	data := map[string]interface{}{
		"name":        p.Name,
		"piece":       p.Piece,
		"prob":        p.Prob,
		"status":      p.Status,
		"type":        p.Type,
		"value":       p.Value,
		"modify_time": time.Now().Unix(),
	}
	_, err := pc.UpdateLotteryProduct(data, p.Id)
	if err != nil {
		LogPc.Error(b.DatabaseError(err.Error()))
		return
	}
	msg := fmt.Sprintf("更新奖品(%v,奖品ID:%v)", p.Name, p.Id)
	b.NewLog(LogLottery, OpUpdate, msg, data)
	b.ResponseSuccess()
}

// @Title 抽奖奖品列表
// @Description 抽奖奖品列表
// @Param   page_size    body   int     true       "每页显示数量"
// @Param   page_index   body   int     true       "第几页"
// @Success 0 {object} [] 奖品列表
// @router /product/list-lottery-product [post]
func (b *LotteryController) ListLotteryProduct() {
	type Params struct {
		ListParams
	}
	var p Params
	if !b.DecodeParams(&p) {
		return
	}
	if !b.ValidParams(&p) {
		return
	}
	data, count, err := pc.ListLotteryProduct(p.ListParams)
	if err != nil {
		LogPc.Error(b.DatabaseError(err.Error()))
		return
	}
	b.Res.Count = count
	b.Res.Data = data
	b.ResponseSuccess()
}

// @Title 玩家拿钻石兑换抽奖次数的记录列表
// @Description 玩家拿钻石兑换抽奖次数的记录列表
// @Param   page_size    body   int     true       "每页显示数量"
// @Param   page_index   body   int     true       "第几页"
// @Param   uid          body   int     true       "玩家uid"
// @Success 0 {object} [] 奖品列表
// @router /consume/list-lottery-consume [post]
func (b *LotteryController) ListLotteryConsume() {
	type Params struct {
		ListParams
		Uid int64 `json:"uid" valid:"Required"`
	}
	var p Params
	if !b.DecodeParams(&p) {
		return
	}
	if !b.ValidParams(&p) {
		return
	}
	data, count, err := pc.ListLotteryConsume(p.ListParams, p.Uid)
	if err != nil {
		LogPc.Error(b.DatabaseError(err.Error()))
		return
	}
	b.Res.Count = count
	b.Res.Data = data
	b.ResponseSuccess()
}

// @Title 添加钻石兑换抽奖次数
// @Description 添加钻石兑换抽奖次数
// @Param   diamond         body   int        true       "钻石数"
// @Param   lottery_count   body   int        true       "抽奖次数"
// @Success 0 {string}  状态码
// @router /exchange/new-lottery-exchange [post]
func (b *LotteryController) NewLotteryExchange() {
	var p LotteryExchange
	if !b.DecodeParams(&p) {
		return
	}
	b.Valid.Required(p.Diamond, "diamond").Message("钻石数")
	b.Valid.Required(p.LotteryCount, "lottery_count").Message("抽奖次数")
	if !b.VerifyParamsError() {
		return
	}
	data := map[string]interface{}{
		"diamond":       p.Diamond,
		"lottery_count": p.LotteryCount,
		"create_time":   time.Now().Unix(),
	}
	_, err := pc.NewLotteryExchange(data)
	if err != nil {
		LogPc.Error(b.DatabaseError(err.Error()))
		return
	}
	msg := fmt.Sprintf(" 添加钻石兑换抽奖次数")
	b.NewLog(LogLottery, OpNew, msg, data)
	b.ResponseSuccess()
}

// @Title 更新钻石兑换抽奖次数
// @Description 更新钻石兑换抽奖次数
// @Param   id           body   int        true       "奖品id"
// @Param   diamond         body   int        true       "钻石数"
// @Param   lottery_count   body   int        true       "抽奖次数"
// @Success 0 {string}  状态码
// @router /exchange/update-lottery-exchange [post]
func (b *LotteryController) UpdateLotteryExchange() {
	var p LotteryExchange
	if !b.DecodeParams(&p) {
		return
	}
	b.Valid.Required(p.Id, "id").Message("Id不能为0")
	b.Valid.Required(p.Diamond, "diamond").Message("钻石数")
	b.Valid.Required(p.LotteryCount, "lottery_count").Message("抽奖次数")
	if !b.VerifyParamsError() {
		return
	}
	data := map[string]interface{}{
		"diamond":       p.Diamond,
		"lottery_count": p.LotteryCount,
	}
	_, err := pc.UpdateLotteryExchange(data, p.Id)
	if err != nil {
		LogPc.Error(b.DatabaseError(err.Error()))
		return
	}
	msg := fmt.Sprintf(" 更新钻石兑换抽奖次数(ID:%v)", p.Id)
	b.NewLog(LogLottery, OpUpdate, msg, data)
	b.ResponseSuccess()
}

// @Title 删除钻石兑换抽奖次数
// @Description 删除钻石兑换抽奖次数
// @Param   id           body   int        true       "奖品id"
// @Success 0 {string}  状态码
// @router /exchange/delete-lottery-exchange [post]
func (b *LotteryController) DeleteLotteryExchange() {
	type Params struct {
		Id int64 `json:"id" valid:"Required"`
	}
	var p Params
	if !b.DecodeParams(&p) {
		return
	}
	if !b.ValidParams(&p) {
		return
	}
	_, err := pc.DeleteLotteryExchange(p.Id)
	if err != nil {
		LogPc.Error(b.DatabaseError(err.Error()))
		return
	}
	msg := fmt.Sprintf("删除钻石兑换抽奖次数(ID:%v)", p.Id)
	b.NewLog(LogLottery, OpDelete, msg, map[string]interface{}{})
	b.ResponseSuccess()
}

// @Title 钻石兑换抽奖次数列表
// @Description 钻石兑换抽奖次数列表
// @Param   page_size    body   int     true       "每页显示数量"
// @Param   page_index   body   int     true       "第几页"
// @Success 0 {object} [] 奖品列表
// @router /exchange/list-lottery-exchange [post]
func (b *LotteryController) ListLotteryExchange() {
	type Params struct {
		ListParams
	}
	var p Params
	if !b.DecodeParams(&p) {
		return
	}
	if !b.ValidParams(&p) {
		return
	}
	data, count, err := pc.ListLotteryExchange(p.ListParams)
	if err != nil {
		LogPc.Error(b.DatabaseError(err.Error()))
		return
	}
	b.Res.Count = count
	b.Res.Data = data
	b.ResponseSuccess()
}
